<template>
  <div>
      <div class="shopcart" v-if="totalCount">
          <div class="content">
              <div class="content-left" @click="toggleShow">
                  <div class="logo-wrapper">
                        <div class="logo"> 
                          <img src="./imgs/shopCart.svg">
                        </div>
                       <div class="num" v-if="totalCount">{{totalCount}}</div>
                  </div>
                  <div class="price">￥{{totalPrice}}元</div>
              </div>
              <div class="pay" @click="toPay">{{payDesc}}</div>         
          </div>

          <div class="shopcart-list" v-show="isShow">
              <div class="list-header">
                  <h1 class="title">购物车</h1> 
                  <span class="empty" @click="clearCart">清空购物车</span>
              </div>
              <div class="list-content" ref="listContent">
                  <ul>
                      <li class="food" v-for="(food,index) of cartFoods" :key="index"> 
                          <!-- <img :src="food.icon"> -->
                          <span class="name">{{food.name}}</span>
                          <div class="price">
                              <span>￥{{food.price}}</span>
                          </div>
                          <div class="cartcontrol-wrapper">
                              <div class="cartcontrol">
                                  <CartControl :food='food'/>
                              </div>
                          </div>
                      </li>
                  </ul>
              </div>
          </div>
      </div>
      <div class="list-mask" v-show="listShow" @click="toggleShow"></div>
  </div>
</template>

<script>
import CartControl from "../CartControl/CartControl.vue";
import BScroll from "better-scroll";
import { MessageBox } from "mint-ui";

import { mapState, mapGetters } from "vuex";
import { click } from "@better-scroll/shared-utils";

export default {
  data() {
    return { isShow: false };
  },
  methods: {
    toggleShow() {
      this.isShow = !this.isShow;
    },
    clearCart() {
      MessageBox.confirm(`确定清空购物车吗？`).then(
        action => {
          this.$store.dispatch("clearCart");
        },
        () => {}
      );
    },
    toPay() {
      if (this.totalPrice >= 20) {
        this.$router.push("/settlement");
      }
    }
  },
  computed: {
    ...mapState(["cartFoods"]),
    ...mapGetters(["totalCount", "totalPrice"]),

    payDesc() {
      if (this.totalPrice === 0) {
        return `￥20元起送`;
      } else if (this.totalPrice < 20) {
        let diff = 20 - this.totalPrice;
        return `还差￥${diff}元起送`;
      } else {
        return `去结算`;
      }
    },

    listShow() {
      if (this.totalCount === 0) {
        this.isShow = false;
        return false;
      }
      if (this.isShow) {
        this.$nextTick(() => {
          if (!this.scroll) {
            this.scroll = new BScroll(this.$refs.listContent, {
              click: true,
              scrollY: true
            });
          } else {
            this.scroll.refresh();
          }
        });
        return this.isShow;
      }
    }
  },
  components: {
    CartControl
  }
};
</script>

<style lang="scss" scoped>
ul {
  margin: 0;
  padding: 0;
}
.shopcart {
  position: fixed;
  left: 0;
  bottom: 55px;
  height: 48px;
  width: 100%;
  z-index: 50;
  .content {
    display: flex;
    background-color: #e9e9e9;
    // background-color: red;
    height: 100%;
    .content-left {
      position: absolute;
      width: 50%;
      .logo-wrapper {
        display: inline-block;
        position: absolute;
        top: -10px;
        margin: 0 12px;
        padding: 6px;
        width: 56px;
        height: 56px;
        box-sizing: border-box;
        vertical-align: top;
        border-radius: 50%;
        // background-color: blue;
        background-color: #fff;
        .logo {
          width: 100%;
          height: 100%;
          border-radius: 50%;
          text-align: center;
          img {
            height: 35px;
            width: 35px;
            margin-top: 2px;
          }
        }
        .num {
          position: absolute;
          top: 0;
          right: 0;
          width: 24px;
          height: 16px;
          line-height: 16px;
          font-size: 10px;
          text-align: center;
          background: #fba92a;
          border-radius: 16px;
          box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.5);
          font-weight: 700;
        }
      }
      .price {
        position: absolute;
        top: 15px;
        left: 70px;
        width: 90px;
        font-size: 16px;
        font-weight: 700;
        color: #000;
      }
    }
    .pay {
      position: absolute;
      right: 0;
      box-sizing: border-box;
      height: 48px;
      line-height: 48px;
      z-index: 300;
      background-color: rgb(219, 168, 113);
      padding: 0 30px;
      color: white;
    }
  }
  .shopcart-list {
    position: absolute;
    z-index: -1;
    left: 0;
    bottom: 48px;
    width: 100%;
    // background-color: red;
    .list-header {
      height: 40px;
      line-height: 40px;
      padding: 0 14px;
      background-color: #f3f5f7;
      border-bottom: 1px solid rgba(7, 17, 27, 0.1);
      border-radius: 5px 5px 0 0;
      .title {
        float: left;
        font-size: 16px;
        color: rgb(7, 17, 27);
        font-weight: 700;
        margin-top: 10px;
      }
      .empty {
        float: right;
        font-size: 12px;
        color: rgb(0, 160, 220);
        padding-left: 10px;
        border-left: 1px solid rgba(7, 17, 27, 0.1);
      }
    }
    .list-content {
      padding: 0 15px;
      max-height: 200px;
      overflow: hidden;
      background-color: #fff;
      .food {
        position: relative;
        padding: 6px 0;
        box-sizing: border-box;
        border-bottom: 1px solid rgba(7, 17, 27, 0.1);
        // img {
        //   height: 45px;
        //   width: 45px;
        //   margin: 0 5px;
        // }
        .name {
          line-height: 24px;
          font-size: 14px;
          color: rgb(7, 17, 27);
        }
        .price {
          float: right;
          margin-right: 90px;
          line-height: 24px;
          font-size: 14px;
          font-weight: 700;
        }
        .cartcontrol-wrapper {
          position: absolute;
          right: 5px;
          bottom: 7px;
        }
      }
    }
  }
}
.list-mask {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 55px;
  width: 100%;
  z-index: 20;
  opacity: 1;
  background: rgba(7, 17, 27, 0.6);
}
</style>
